﻿using Microsoft.Extensions.Options;
using System;
using System.Windows;
using Wpf.Ui;
using Wpf.Ui.Controls;
using XC.EFCore.Repositories.Admin.Core;
using XC.Models.Entities.Framework;
using XC.WpfHost.ViewModels;
using XC.WpfHost.Views.Pages;

namespace XC.WpfHost
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : INavigationWindow
    {
        public MainWindowViewModel ViewModel { get; }
        private bool _isUserClosedPane;
        private bool _isPaneOpenedOrClosedFromCode;
        public MainWindow(MainWindowViewModel viewModel, IPageService pageService,
        INavigationService navigationService,ISnackbarService snackbarService)
        {
            ViewModel = viewModel;
            DataContext = this;
            Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this);
            InitializeComponent();
            //ApplicationThemeManager.Apply(ApplicationTheme.Dark);
            snackbarService.SetSnackbarPresenter(SnackbarPresenter);
            SetPageService(pageService);
            navigationService.SetNavigationControl(RootNavigation);
        }

        #region INavigationWindow methods

        public INavigationView GetNavigation() => RootNavigation;

        public bool Navigate(Type pageType) => RootNavigation.Navigate(pageType);

        public void SetPageService(IPageService pageService) => RootNavigation.SetPageService(pageService);

        public void ShowWindow() => Show();

        public void CloseWindow() => Close();

        #endregion INavigationWindow methods

        /// <summary>
        /// Raises the closed event.
        /// </summary>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Make sure that closing this window will begin the process of closing the application.
            Application.Current.Shutdown();
        }

        INavigationView INavigationWindow.GetNavigation()
        {
            throw new NotImplementedException();
        }

        public void SetServiceProvider(IServiceProvider serviceProvider)
        {
            throw new NotImplementedException();
        }

        private void NavigationView_OnPaneClosed(NavigationView sender, RoutedEventArgs args)
        {
            if (sender is not Wpf.Ui.Controls.NavigationView navigationView)
            {
                return;
            }

            RootNavigation.HeaderVisibility =
                navigationView.SelectedItem?.TargetPageType != typeof(DashboardPage)
                    ? Visibility.Visible
                    : Visibility.Collapsed;
        }

        private void NavigationView_OnPaneOpened(NavigationView sender, RoutedEventArgs args)
        {
            if (_isPaneOpenedOrClosedFromCode)
            {
                return;
            }

            _isUserClosedPane = false;
        }

        private void OnNavigationSelectionChanged(NavigationView sender, RoutedEventArgs args)
        {
            if (_isPaneOpenedOrClosedFromCode)
            {
                return;
            }

            _isUserClosedPane = true;
        }
    }
}